// page/searchresult/series/index.js
var loadSearchResult = require('../loadSearchResult.js');
var app = getApp();
var tab_page = {};
var page = 0;
var size = 12 ;
var inputVal='';
var username="";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    series_list: [],
    list:[],
    keywords: '',
    windowHeight:0,
    lastLoadTime: 0,
    clickSeriesId:0//目的是防止重复点击
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var keywords = options.keywords;
    inputVal = keywords;
    tab_page={};
    var data = {
      page: page,
      size: size,
      keywords: keywords
    };


    loadSearchResult.search_series_page(data, tab_page, that);
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight - 30,
          windowWidth: res.windowWidth,
          keywords: keywords
        })
      }
    });

    loadSearchResult.load_materials(keywords, username, that);
    if (username) {
      loadSearchResult.load_materials(keywords, username, that);
    } else {
      app.getUserName(function (user_name) {
        username = user_name;
        loadSearchResult.load_materials(keywords, username, that);
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: config.shareDes,
      path: '/pages/index/index',
      imageUrl: '../images/sharepic_1.jpg'
    }
    // return {
    //   title: config.shareName,
    //   desc: config.shareDes,
    //   path: '/pages/searchlist/series/index?keywords='
    // }
  },
  loadMore: function () {
    var that=this;
    var currentPage = tab_page.page.number + 1;
    if (currentPage <= tab_page.page.totalPages - 1) {
      var curTime = new Date().getTime();
      //上一次加载的时间
      var lastTime = this.data.lastLoadTime;
      that.setData({
        lastLoadTime: curTime
      });
      if (curTime - lastTime < 300) {
        return;
      }
      var data = {
        page: currentPage,
        size: size,
        keywords: tab_page.keywords
      };
      wx.showToast({
        title: '加载中...',
        icon: 'loading',
        duration: 1000
      });
      loadSearchResult.search_series_page(data, tab_page, that);
    }

  }, refresh: function () {

  },inplay_series:function(e){
    var id=e.currentTarget.dataset.id;
    if(id==0||id==null){
      return;
    }
    if (this.data.clickSeriesId==id){
      return;
    }
    this.setData({
      clickSeriesId:id
    });
    wx.navigateTo({
      url: '../../search/seriesList/serieslist?id='+id,
    })
  }, search_input:function(e){
    var keywords = e.detail.value;
    var that = this;
    tab_page = {};
    if (!keywords) {
      that.setData({
        series_list: []
      });
      return;
    }
    var data = {
      page: 0,
      size: size,
      keywords: keywords
    };
  
    loadSearchResult.search_series_page(data, tab_page, that);

    if (username) {
      loadSearchResult.load_materials(keywords, username, that);
    } else {
      app.getUserName(function (user_name) {
        username = user_name;
        loadSearchResult.load_materials(keywords, username, that);
      });
    }


  },more_materials:function(e){
      var keywords=e.currentTarget.dataset.keywords;
      wx.redirectTo({
        url: '../searchlist?keywords='+keywords
      })
  }

})